package net.huashitong.Project.web;

import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.entity.RelocatePlan;
import net.huashitong.Project.entity.RelocateProgress;
import net.huashitong.Project.entity.RelocateSummary;
import net.huashitong.Project.filter.RelocatePlanFilter;
import net.huashitong.Project.filter.RelocateProgressFilter;
import net.huashitong.Project.filter.RelocateSummaryFilter;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.Project.service.RelocatePlanService;
import net.huashitong.Project.service.RelocateProgressService;
import net.huashitong.Project.service.RelocateSummaryService;
import net.huashitong.user.entity.BnpUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.huashitong.utils.UtilsTools.getCurrentMonth;

@Controller
@RequestMapping(value = "/web/relocate")
public class RelocateController {

    Logger logger = LoggerFactory.getLogger("RelocateController");

    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private RelocatePlanService planService;
    @Autowired
    private RelocateProgressService progressService;
    @Autowired
    private RelocateSummaryService summaryService;

    //=============================计划=============================

    /**
     * 查询指定项目的征迁计划
     *
     * @param projectId
     * @param model
     * @return
     */
    @RequestMapping(value = "/queryPlansByProjectId")
    public String queryPlansByProjectId(@RequestParam String projectId, Model model) {
        model.addAttribute("leftType", "我的项目");
        NewProjects newProject = newProjectsService.getById(projectId);
        List<RelocatePlan> plans = planService.queryByProjectId(projectId);
        List<RelocateProgress> list = progressService.queryByProjectId(projectId);

        model.addAttribute("newProject", newProject);
        if (plans.size() == 0) {
            model.addAttribute("plans", "");
        }else{
            model.addAttribute("plans", plans);
        }
        model.addAttribute("list", list);
        //当前月
        String currentMonth = getCurrentMonth();
        model.addAttribute("currentMonth",currentMonth.substring(0,4)+"-"+currentMonth.substring(4,6));

        return "/modules/relocate/relocateIndex";
    }

    /**
     * 添加或者修改征迁计划
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRelocatePlan", method = RequestMethod.POST)
    public Map<String, Object> addRelocatePlan(@RequestBody RelocatePlanFilter filter, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if(ObjectUtils.isBlank(filter.getDataTime())||
                ObjectUtils.isBlank(filter.getPlanData())||
                ObjectUtils.isBlank(filter.getContent())){
            result.put("code", "201");
            result.put("message", "提交失败,请按照要求填写。");
            return result;
        }

        planService.addRelocatePlan(filter, bnpUser);
        result.put("code", "200");
        result.put("message", filter.getDataTime() + "征迁计划，操作成功");
        return result;
    }

    /**
     * 删除征迁计划
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deletePlan/{id}", method = RequestMethod.GET)
    public Map<String, Object> deletePlan(@PathVariable List<String> id) {
        planService.batchDelete(id);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功删除征迁计划");
        return result;
    }


    //=============================进度=============================

    /**
     * 查询指定项目的征迁进度
     *
     * @param projectId
     * @param model
     * @return
     */
    @RequestMapping(value = "/queryProgressByProjectId")
    public String queryProgressByProjectId(@RequestParam String projectId, Model model) {
        List<RelocateProgress> list = progressService.queryByProjectId(projectId);
        return "/modules/relocate/relocateIndex";
    }

    /**
     * 添加或者修改征迁进度
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRelocateProgress", method = RequestMethod.POST)
    public Map<String, Object> addRelocateProgress(@RequestBody RelocateProgressFilter filter, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if(ObjectUtils.isBlank(filter.getDataTime())||
                ObjectUtils.isBlank(filter.getProgressData())||
                ObjectUtils.isBlank(filter.getProblem())||
                ObjectUtils.isBlank(filter.getNextPlan())||
                ObjectUtils.isBlank(filter.getProgressDescription())){
            result.put("code", "201");
            result.put("message", "提交失败,请按照要求填写。");
            return result;
        }
        progressService.addRelocateProgress(filter, bnpUser);
        result.put("code", "200");
        result.put("message",  filter.getDataTime() + "征迁进度，操作成功");
        return result;
    }

    /**
     * 删除征迁进度
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteProgress/{id}")
    public Map<String, Object> deleteProgress(@PathVariable List<String> id) {
        progressService.batchDelete(id);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功删除征迁进度");
        return result;
    }


    //=============================汇总=============================

    /**
     * 查询指定项目的征迁汇总(综述)
     *
     * @param projectId
     * @param model
     * @return
     */
    @RequestMapping(value = "/querySummarysByProjectId")
    public String querySummarysByProjectId(@RequestParam String projectId, Model model) {
        List<RelocateSummary> list = summaryService.queryByProjectId(projectId);
        return "/modules/buildProjects/buildProjectRecords";
    }

    /**
     * 添加或者修改征迁汇总(综述)
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRelocateSummary", method = RequestMethod.POST)
    public Map<String, Object> addRelocateSummary(@RequestBody RelocateSummaryFilter filter) {
        summaryService.addRelocateSummary(filter);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "添加或者修改征迁计划");
        return result;
    }

    /**
     * 删除征迁汇总(综述)
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteSummary/{id}")
    public Map<String, Object> deleteSummary(@PathVariable List<String> id) {
        summaryService.batchDelete(id);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功删除征迁计划");
        return result;
    }
}
